package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
public class WebDetailController {
    @Autowired
    private ProductFeignClient productFeignClient;
//    @Autowired
//    private SearchFeignClient searchFeignClient;
    @Autowired
    private ThreadPoolExecutor myExecutor;
    @GetMapping("webSkuInfo/{skuId}")
    @ResponseBody
    public SkuInfo webSkuInfo(@PathVariable Long skuId){
        return productFeignClient.getSkuInfo(skuId);
    }

    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){

        Map<String, Object> dataMap = new HashMap<>();
        //发起异步请求 没有返回值
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            //3.根据skuId获取商品的实时价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            dataMap.put("price", skuPrice);
        },myExecutor);
        //发起异步请求 有返回值
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1.根据商品skuId查询商品基本信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            dataMap.put("skuInfo", skuInfo);
            return skuInfo;
        },myExecutor);

        //必须要在获取到商品基本信息以后 才去采用并行
        CompletableFuture<Void> categoryViewFuture = skuInfoFuture.thenAcceptAsync((skuInfo -> {
            //2.根据三级分类id获取商品的分类信息
            Long category3Id = skuInfo.getCategory3Id();
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            dataMap.put("categoryView", categoryView);
        }),myExecutor);

        CompletableFuture<Void> salePropertyListFuture = skuInfoFuture.thenAcceptAsync((skuInfo -> {
            //4.获取该sku所有的spu销售属性(很多)与sku的销售属性(一个)
            Long productId = skuInfo.getProductId();
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSaleProperteyAndSelected(productId, skuId);
            dataMap.put("spuSalePropertyList",spuSalePropertyList);
        }),myExecutor);

        CompletableFuture<Void> salePropertyAndSkuIdFuture =skuInfoFuture.thenAcceptAsync((skuInfo -> {
            //5.查询销售属性组合与skuid的一个对应关系
            Long productId = skuInfo.getProductId();
            Map<Object, Object> salePropertyAndSkuIdMapping = productFeignClient.getSalePropertyAndSkuIdMapping(productId);
            //我们以前用的是@ResponseBody-->把对象转换为json字符串
            dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyAndSkuIdMapping));
        }),myExecutor);


//        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
//            searchFeignClient.incrHotScore(skuId);
//        },myExecutor);

        //多任务组合 必须这几个任务全部执行完拿到数据以后 才能把数据赋值给页面
        CompletableFuture.allOf(priceFuture,
                skuInfoFuture,
                categoryViewFuture,
                salePropertyListFuture,
                salePropertyAndSkuIdFuture).join();
        //注意不要写成addAllAttribute
        model.addAllAttributes(dataMap);
        return "detail/index";
    }

//    @RequestMapping("{skuId}.html")
//    public String getSkuDetail(@PathVariable Long skuId, Model model){
//        Map<String, Object> dataMap = new HashMap<>();
//        //1.根据商品skuId查询商品基本信息
//        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//        dataMap.put("skuInfo",skuInfo);
//        //2.根据三级分类id获取商品的分类信息
//        Long category3Id = skuInfo.getCategory3Id();
//        BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
//        dataMap.put("categoryView",categoryView);
//        //3.根据skuId获取商品的实时价格
//        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
//        dataMap.put("price",skuPrice);
//        //4.获取该sku所有的spu销售属性(很多)与sku的销售属性(一个)
//        Long productId = skuInfo.getProductId();
//        List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSaleProperteyAndSelected(productId, skuId);
//        dataMap.put("spuSalePropertyList",spuSalePropertyList);
//        //5.查询销售属性组合与skuid的一个对应关系
//        Map<Object, Object> salePropertyAndSkuIdMapping = productFeignClient.getSalePropertyAndSkuIdMapping(productId);
//        //我们以前用的是@ResponseBody-->把对象转换为json字符串
//        dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyAndSkuIdMapping));
//        //注意不要写成addAllAttribute
//        model.addAllAttributes(dataMap);
//        return "detail/index";
//    }

}
